var Toast = require("toast");

cc.Class({
    extends: cc.Component,

    properties: {
       number_perfab: cc.Prefab,
       gamemode_prefab: cc.Prefab,
       announcement_prefab: cc.Prefab,
       headbiankuang_prefab: cc.Prefab,
       exploits_prefab: cc.Prefab,
       setting_prefab: cc.Prefab,
       tips_prefab: cc.Prefab,
       howtoplay_prefab: cc.Prefab,
       
       createroom: cc.SpriteFrame,
       joinroom: cc.SpriteFrame,
       fanhui: cc.SpriteFrame,
       tuichu: cc.SpriteFrame,
    },
    
    lobby_static: 0,
    move_static: 0,

    onLoad: function () {
        this.onReadyShowScene();
        this.onAnnouncement();
        this.onShowUserInfo();
    },
    
    //预备显示场景的准备工作
    onReadyShowScene: function () {
        this.node.opacity = 0;
        this.node.runAction(cc.fadeTo(0.2, 255));
        var createroom = cc.find("spr_bg/node_firstlayer/btn_createroom", this.node);
        createroom.active = false;
        var joinroom = cc.find("spr_bg/node_firstlayer/btn_joinroom", this.node);
        joinroom.active = false;
    },
    
    //初始化用户信息
    onShowUserInfo: function () {
        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        
        //头像边框动画
        var headbiankuang_node = cc.find("spr_bg/node_userinfo/head_kuang", this.node);
        var headbiankuang_prefab = cc.instantiate(this.headbiankuang_prefab);
        headbiankuang_prefab.getComponent(cc.Animation).play('biankuang');
        headbiankuang_node.addChild(headbiankuang_prefab);
        //用户名
        var username_node = cc.find("spr_bg/node_userinfo/label_username", this.node);
        username_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.username;
        username_node = cc.find("spr_bg/node_infolayer/label_tousername", this.node);
        username_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.username;
        //钻石
        var diamond_node = cc.find("spr_bg/node_userinfo/label_zuanshinum", this.node);
        diamond_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.diamond;
        diamond_node = cc.find("spr_bg/node_infolayer/label_tozuanshi", this.node);
        diamond_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.diamond;
        //ID
        var id_node = cc.find("spr_bg/node_infolayer/label_toid", this.node);
        id_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.uid;
        //总场次
        var allseason_node = cc.find("spr_bg/node_infolayer/label_toallround", this.node);
        allseason_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.allseason;
        //胜利
        var winnum_node = cc.find("spr_bg/node_infolayer/label_towinround", this.node);
        winnum_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.winnum;
        //返利
        var fanli_node = cc.find("spr_bg/node_infolayer/label_tofanli", this.node);
        fanli_node.getComponent(cc.Label).string = gamedata_component.myuserinfo.fanli;
    },
    
    //添加滚动公告
    onAnnouncement: function () {
        var gongneng_node = cc.find("spr_bg/node_gongneng", this.node);
        var announcement_prefab = cc.instantiate(this.announcement_prefab);
        announcement_prefab.setPosition(cc.p(0, gongneng_node.getPositionY() - announcement_prefab.height));
        this.node.addChild(announcement_prefab);
    },
    
    //点击十三水
    onShiSanShui: function () {
        this.lobby_static = 1;
        var btn_moregame = cc.find("spr_bg/node_firstlayer/btn_moregame", this.node);
        btn_moregame.active = false;
        var btn_shisanshui = cc.find("spr_bg/node_firstlayer/btn_shisanshui", this.node);
        btn_shisanshui.active = false;
        
        //退出变返回
        var fanhui_node = cc.find("spr_bg/node_gongneng/btn_fanhui", this.node);
        fanhui_node.getComponent(cc.Sprite).spriteFrame = this.fanhui;
        
        var node_firstlayer = cc.find("spr_bg/node_firstlayer", this.node);
        var move_shisanshui = new cc.Node();
        move_shisanshui.name = "move_shisanshui";
        var spr_move_shisanshui = move_shisanshui.addComponent(cc.Sprite);
        spr_move_shisanshui.getComponent(cc.Sprite).spriteFrame = btn_shisanshui.getComponent(cc.Sprite).spriteFrame;
        move_shisanshui.setPosition(cc.p(btn_shisanshui.getPosition()));
        node_firstlayer.addChild(move_shisanshui);
        move_shisanshui.runAction(cc.sequence(
            cc.moveTo(0.3, cc.p(btn_moregame.getPositionX(), btn_moregame.getPositionY())), 
            cc.callFunc(this.moveShisanshui, this)
            ));
    },
    
    //十三水执行的动作
    moveShisanshui: function () {
        var node_firstlayer = cc.find("spr_bg/node_firstlayer", this.node);
        if (node_firstlayer.getChildByName("move_shisanshui")) {
            node_firstlayer.getChildByName("move_shisanshui").destroy();
        }
        var btn_moregame = cc.find("spr_bg/node_firstlayer/btn_moregame", this.node);

        var createroom = cc.find("spr_bg/node_firstlayer/btn_createroom", this.node);
        createroom.active = true;
        createroom.runAction(cc.moveTo(0.5, cc.p(btn_moregame.getPositionX(), btn_moregame.getPositionY() + 100)));
        var joinroom = cc.find("spr_bg/node_firstlayer/btn_joinroom", this.node);
        joinroom.active = true;
        joinroom.runAction(cc.moveTo(0.5, cc.p(btn_moregame.getPositionX(), btn_moregame.getPositionY() - 150)));
        var lady = cc.find("spr_bg/node_firstlayer/spr_lady", this.node);
        lady.opacity = 255;
    },
    
    //更多游戏
    onMoreGame: function () {
        Toast.show("敬请期待", this.node);
    },
    
    //创建房间
    onCreateRoom: function () {
        var gamemode_prefab = cc.instantiate(this.gamemode_prefab);
        this.node.addChild(gamemode_prefab);
    },
    
    //加入房间
    onJoinRoom: function () {
        var number_perfab = cc.instantiate(this.number_perfab);
        this.node.addChild(number_perfab);
    },
    
    //玩法
    onWanfa: function () {
        var howtoplay_prefab = cc.instantiate(this.howtoplay_prefab);
        this.node.addChild(howtoplay_prefab);
    },
    
    //战绩
    onZhanji: function () {
        var exploits_prefab = cc.instantiate(this.exploits_prefab);
        this.node.addChild(exploits_prefab);
    },
    
    //设置
    onShezhi: function () {
        var setting_prefab = cc.instantiate(this.setting_prefab);
        this.node.addChild(setting_prefab);
    },
    
    //添加钻石
    onAddZuanshi: function () {
        var tips_prefab = cc.instantiate(this.tips_prefab);
        tips_prefab.getComponent("tips_component").tips_type = 2;
        tips_prefab.getComponent("tips_component").label_text = "\n\n\n购买钻石请联系\n【友高娱乐】\n微信公众号";
        this.node.addChild(tips_prefab);
    },
    
    //返回
    onFanhui: function () {
        if (this.lobby_static) {
            var createroom = cc.find("spr_bg/node_firstlayer/btn_createroom", this.node);
            createroom.active = false;
            var joinroom = cc.find("spr_bg/node_firstlayer/btn_joinroom", this.node);
            joinroom.active = false;
            var lady = cc.find("spr_bg/node_firstlayer/spr_lady", this.node);
            lady.opacity = 0;
            
            var btn_moregame = cc.find("spr_bg/node_firstlayer/btn_moregame", this.node);
            btn_moregame.active = true;
            var btn_shisanshui = cc.find("spr_bg/node_firstlayer/btn_shisanshui", this.node);
            btn_shisanshui.active = true;
            
            //返回变退出
            var fanhui_node = cc.find("spr_bg/node_gongneng/btn_fanhui", this.node);
            fanhui_node.getComponent(cc.Sprite).spriteFrame = this.tuichu;
        
            this.lobby_static = 0;
        } else {
            var tips_prefab = cc.instantiate(this.tips_prefab);
            tips_prefab.getComponent("tips_component").tips_type = 1;
            tips_prefab.getComponent("tips_component").label_text = "是否退到登录界面";
            this.node.addChild(tips_prefab);
        }
        
    },
    
    //玩家信息
    onUserInfo: function () {
        this.showInfo();
    },
    
    //显示用户信息的动作
    showInfo: function () {
        if (this.move_static) {
            return;
        }
        this.move_static = 1;
        var node_firstlayer = cc.find("spr_bg/node_firstlayer", this.node);
        var node_gongneng = cc.find("spr_bg/node_gongneng", this.node);
        var node_leftlayer = cc.find("spr_bg/node_leftlayer", this.node);
        var node_userinfo = cc.find("spr_bg/node_userinfo", this.node);
        var node_infolayer = cc.find("spr_bg/node_infolayer", this.node);
        
        node_firstlayer.runAction(cc.moveBy(0.5, cc.p(0, -600)));
        node_gongneng.runAction(cc.moveBy(0.5, cc.p(0, 150)));
        node_leftlayer.runAction(cc.moveBy(0.5, cc.p(266, 0)));
        node_userinfo.runAction(cc.moveBy(0.5, cc.p(0, 150)));
        node_infolayer.runAction(cc.moveBy(0.5, cc.p(-1000, 0)));
    },
    
    //返回大厅的动作
    returnInfo: function () {
        if (!this.move_static) {
            return;
        }
        this.move_static = 0;
        var node_firstlayer = cc.find("spr_bg/node_firstlayer", this.node);
        var node_gongneng = cc.find("spr_bg/node_gongneng", this.node);
        var node_leftlayer = cc.find("spr_bg/node_leftlayer", this.node);
        var node_userinfo = cc.find("spr_bg/node_userinfo", this.node);
        var node_infolayer = cc.find("spr_bg/node_infolayer", this.node);
        
        node_firstlayer.runAction(cc.moveBy(0.5, cc.p(0, 600)));
        node_gongneng.runAction(cc.moveBy(0.5, cc.p(0, -150)));
        node_leftlayer.runAction(cc.moveBy(0.5, cc.p(-266, 0)));
        node_userinfo.runAction(cc.moveBy(0.5, cc.p(0, -150)));
        node_infolayer.runAction(cc.moveBy(0.5, cc.p(1000, 0)));
    },
});
